package com.hangwei.paperless.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hangwei.paperless.R;

/**
 * Created by 张龙臣 on 2020/10/12.
 */

public abstract class MainListPopupWindow extends PopupWindow implements View.OnClickListener {

    private final Activity mActivity;
    private final boolean mShadow;

    public MainListPopupWindow(Activity context, boolean shadow) {
        super(context);
        mActivity = context;
        mShadow = shadow;
        View view = LayoutInflater.from(context).inflate(R.layout.list_pop, null);
        TextView tvApply = (TextView) view.findViewById(R.id.tvApply);
        LinearLayout llEPA = (LinearLayout) view.findViewById(R.id.llEPA);
        tvApply.setOnClickListener(this);
        llEPA.setOnClickListener(this);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        // 设置背景
        setBackgroundDrawable(new ColorDrawable());
        // 外部点击事件
        setOutsideTouchable(true);
        setFocusable(true);
        if (shadow)
            setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                    lp.alpha = 1f;
                    mActivity.getWindow().setAttributes(lp);
                }
            });
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (mShadow) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.alpha = 0.7f;
            mActivity.getWindow().setAttributes(lp);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (mShadow) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.alpha = 0.7f;
            mActivity.getWindow().setAttributes(lp);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        if (mShadow) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.alpha = 0.7f;
            mActivity.getWindow().setAttributes(lp);
        }
        super.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvApply:
                showApply();
                break;
            case R.id.llEPA:
                showEPA();
                break;
        }
    }

    public abstract void showEPA();

    public abstract void showApply();
}
